package practice07;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: practice07
 * @ClassName: StudentMar
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019/12/18 9:10
 * @Copyright 大牛版板所有
 */
public class StudentMar {
    static Set<Student> set = new HashSet<>();

    static {
        set.add(new Student("张三",1000,19));
        set.add(new Student("李四",1001,20));
        set.add(new Student("王五",1002,19));
        set.add(new Student("赵六",1003,21));
    }


    // 增加学员信息
    public static void add(Student student){
        set.add(student);
    }

    // 根据学号删除学生
    public static void delete(int id){
        boolean flag = false;
        Iterator<Student> iter = set.iterator();
        while (iter.hasNext()){
            Student st = iter.next();
            if (st.getId() == id){
                iter.remove();
                flag = true;
            }
        }

        if (flag){
            System.out.println("删除成功！");
        }
        else {
            System.out.println("没有这个人！");
        }
    }

    // 根据学号修改学生
    public static void amend(Student student){
        boolean flag = false;
        for (Student st:set) {
            if (st.getId() == student.getId()){
                st.setId(student.getId());
                st.setName(student.getName());
                st.setAge(student.getAge());
                flag = true;
            }
        }
        if (flag){
            System.out.println("修改成功！");
        }
        else {
            System.out.println("没有这个人！");
        }
    }

    // 根据姓名模糊查询学生
    public static void refer(String name){
        for (Student st:set) {
            if (st.getName().equals(name)){
                set.remove(st);
                System.out.println(st.getName()+"\t"+st.getId()+"\t"+st.getAge());
            }
        }
    }

    // 显示学员信息
    public static void showAll(){
        for (Student st:set) {
            System.out.println(st.getName()+"\t"+st.getId()+"\t"+st.getAge());
        }
    }
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String isExit = "y";
        while ("y".equals(isExit)){
            System.out.println("==============欢迎使用学生信息管理系统================");
            System.out.println("|                 1、显示学员信息                      |");
            System.out.println("|                 2、增加学员信息                      |");
            System.out.println("|                 3、删除学员信息                      |");
            System.out.println("|                 4、修改学员信息                      |");
            System.out.println("|                 5、查询学员信息                      |");
            System.out.println("|                 0、退出管理系统                      |");
            System.out.print("请选择功能：");
            int fun = input.nextInt();
            switch (fun){
                case 1:
                    showAll();
                    break;
                case 2:
                    Student stu1 = new Student();
                    System.out.print("请输入增加的学员姓名：");
                    stu1.setName(input.next());
                    System.out.print("请输入增加的学员学号：");
                    stu1.setId(input.nextInt());
                    System.out.print("请输入增加的学员年龄：");
                    stu1.setAge(input.nextInt());
                    add(stu1);
                    System.out.println("增加学员信息成功！");
                    break;
                case 3:
                    System.out.print("请输入要删除分学员学号：");
                    int id = input.nextInt();
                    delete(id);
                    break;
                case 4:
                    Student stu2 = new Student();
                    System.out.print("请输入修改的学员学号：");
                    stu2.setId(input.nextInt());
                    System.out.print("请输入修改的学员新姓名：");
                    stu2.setName(input.next());
                    System.out.print("请输入修改的学员新年龄：");
                    stu2.setAge(input.nextInt());
                    amend(stu2);
                    break;
                case 5:
                    System.out.print("请输入要查询学员的姓名：");
                    String name = input.next();
                    System.out.println("查询的学员信息如下：");
                    refer(name);
                    break;
                case 0:
                    isExit = "n";
                    break;
                default:
                    System.out.println("你选择的功能不存在！");
                    break;
            }
            if ("n".equals(isExit)){
                break;
            }
            System.out.print("请输入是否继续（y/n）:");
            isExit = input.next();
        }
        System.out.println("退出学生管理系统！");
    }
}
